package com.lzx.iteam;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.*;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.lzx.iteam.bean.TalkListData;
import com.lzx.iteam.net.*;
import com.lzx.iteam.dimensioncode.CaptureActivity;
import com.lzx.iteam.util.*;
import com.lzx.iteam.widget.TextMoreView;
import io.rong.imageloader.core.ImageLoader;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.*;


/**
 * Created by hfx on 14-12-24.
 */
public class FunctionFragment extends Fragment implements View.OnClickListener {

    private MainActivity mActivity;
    private PreferenceUtil mPreferenceUtil;
    private AllDialogUtil mDialogUtil;
    private Dialog mOnclicNoAlertDialog;
    private TextMoreView mKeyPad;
    private TextMoreView mySpace;
    private TextMoreView myCircle;

    private TextMoreView mDraft;
    private TextMoreView mRecommend;
    private TextMoreView mSetting;
    private TextMoreView mSchedule;
    private TextMoreView mWeekly;
    private TextMoreView mAttendance;
    private TextMoreView mApproval;
    private TextMoreView mPayTools;
    private TextView mTvScheduleNew;
    private TextView mTvApprovalNew;
    private TextView mTvWeeklyNew;

    private RelativeLayout mCircleNewMessage;
    private ImageView mCircleNewImage;
    private TextView mName;
    private String mMyName;
    private String mMyAvatar;
    private String mMySex;

    private TextView mTvScanf;
    private LinearLayout mMore;
    private TextView mCardName;
    private TextView mCardPhone;
    private TextView mCardEmail;
    private TextView mCardAddress;
    private TextView mCardCompany;
    private TextView mCardPosition;
    private TextView mTvMessage;
    private ImageView mCardPhoto;
    private ImageView mCardSex;
    private PopupWindow mMoreWindow;
    private PopupWindow mShareWindow;

    private ShareUtil mShareUtil;
    private ImageLoader mImageLoader;
    private ArrayList<String> nameCard;
    private String mUid = "";
    private int mNewDrafts;

    private final int GET_TALK_NEW = 1000;
    private final int GET_CALENDAR_STATUS = 1001;
    private final int MSG_GET_NAMECARD = 1003;

    private String newTalkId = "";
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GET_TALK_NEW:
                    if (msg.arg1 == 0) {
                        TalkListData talkListData = (TalkListData) msg.obj;
                        if (talkListData != null && !StringUtil.isEmpty(talkListData.talkId) && !mUid.equals(talkListData.uId)) {
                            mCircleNewMessage.setVisibility(View.VISIBLE);
                            mImageLoader.displayImage(talkListData.uImage, mCircleNewImage, ImageLoaderInterface.optionHeadImage);
                            newTalkId = talkListData.talkId;
                        } else {
                            mCircleNewMessage.setVisibility(View.GONE);
                        }
                    } else {
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(mActivity, R.string.active_failure_no_network,
                                  Toast.LENGTH_LONG).show();
                        } else if (msg.arg1 == 1002 || msg.arg1 == 1003 || msg.arg1 == 1005) {//其他接口也应该加此判断，当前其他接口未加
                            mDialogUtil.titleMsgBtnStyle("提示", (String) msg.obj, mActivity.getString(R.string.ok));
                            mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {

                                @Override
                                public void onConfirmClick(Dialog lDialog) {

                                    lDialog.dismiss();
                                    Intent intent = new Intent(mActivity, LoginActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    mActivity.startActivity(intent);
                                    mActivity.finish();
                                }

                                @Override
                                public void onCancelClick(Dialog lDialog) {
                                }
                            });
                        } else {
                            Toast.makeText(mActivity, (String) msg.obj,
                                  Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
                case GET_CALENDAR_STATUS:
                    if (msg.arg1 == 0) {
                        int msg1 = (Integer) msg.obj;
                        setNewMsg(mTvScheduleNew, msg1);
                    } else if (msg.arg1 == 1) {
                        int msg2 = (Integer) msg.obj;
                        setNewMsg(mTvApprovalNew, msg2);
                    } else if (msg.arg1 == 2) {
                        int msg3 = (Integer) msg.obj;
                        setNewMsg(mTvWeeklyNew, msg3);
                    }

                    break;
                case MSG_GET_NAMECARD:
                    nameCard = (ArrayList<String>) mPreferenceUtil.getCloudContact(Constants.CONTACT_NAME_CARD);
                    if (nameCard != null) {
                        setNameCard(nameCard);
                    }
                    break;

            }
        }
    };

    /**
     * 设置未读消息数目
     *
     * @param textView
     * @param status
     */
    private void setNewMsg(TextView textView, int status) {
        if (status == 0) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            if (status > 0 && status < 10) {
                ViewGroup.LayoutParams linearParams = textView.getLayoutParams();
                int pd = Constants.dip2px(mActivity, 20);
                linearParams.width = pd;
                linearParams.height = pd;
                textView.setLayoutParams(linearParams);
                textView.setBackgroundResource(R.drawable.unread_1);
                textView.setGravity(Gravity.CENTER);
                textView.setText(status + "");
            } else if (status >= 10 && status < 100) {
                ViewGroup.LayoutParams linearParams = textView.getLayoutParams();
                int widthPx = Constants.dip2px(mActivity, 30);
                int heightPx = Constants.dip2px(mActivity, 20);
                linearParams.width = widthPx;
                linearParams.height = heightPx;
                textView.setLayoutParams(linearParams);
                textView.setBackgroundResource(R.drawable.unread_2);
                textView.setText(status + "");
            } else if (status >= 100) {
                ViewGroup.LayoutParams linearParams = textView.getLayoutParams();
                int widthPx = Constants.dip2px(mActivity, 40);
                int heightPx = Constants.dip2px(mActivity, 20);
                linearParams.width = widthPx;
                linearParams.height = heightPx;
                textView.setLayoutParams(linearParams);
                textView.setBackgroundResource(R.drawable.unread_3);
                textView.setText("99+");
            }
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity) getActivity();
        mPreferenceUtil = PreferenceUtil.getInstance(mActivity);
        mUid = mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "");
        mDialogUtil = AllDialogUtil.getInstance(mActivity);
        mShareUtil = ShareUtil.getInstance(mActivity);
        mImageLoader = ImageLoader.getInstance();
        nameCard = (ArrayList<String>) mPreferenceUtil.getCloudContact(Constants.CONTACT_NAME_CARD);
        if (nameCard == null) {
            uploadNameCard();
        }
        Log.d("function", "oncreate");

    }

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.function_frag_layout, container, false);
            initView(rootView);
        }
        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    public void initView(View view) {

//        mKeyPad = (TextMoreView) view.findViewById(R.id.tm_keypad);
//        mKeyPad.setOnClickListener(this);
        myCircle = (TextMoreView) view.findViewById(R.id.tm_circle);
        myCircle.setOnClickListener(this);
        mySpace = (TextMoreView) view.findViewById(R.id.tm_my_space);
        mySpace.setOnClickListener(this);
        mSchedule = (TextMoreView) view.findViewById(R.id.tm_schedule);
        mSchedule.setOnClickListener(this);
        mWeekly = (TextMoreView) view.findViewById(R.id.tm_weekly);
        mWeekly.setOnClickListener(this);
        mAttendance = (TextMoreView) view.findViewById(R.id.tm_attendance);
        mAttendance.setOnClickListener(this);
        mApproval = (TextMoreView) view.findViewById(R.id.tm_approval);
        mApproval.setOnClickListener(this);
        mRecommend = (TextMoreView) view.findViewById(R.id.tm_recommend);
        mRecommend.setOnClickListener(this);
        mSetting = (TextMoreView) view.findViewById(R.id.tm_setting);
        mSetting.setOnClickListener(this);
        mDraft = (TextMoreView) view.findViewById(R.id.tm_draft);
        mDraft.setOnClickListener(this);
        mPayTools = (TextMoreView) view.findViewById(R.id.tm_pay_tools);
        mPayTools.setOnClickListener(this);
        mName = (TextView) view.findViewById(R.id.tv_namecard_name);
        mTvMessage = (TextView) view.findViewById(R.id.draft_tv_message);
        mCircleNewMessage = (RelativeLayout) view.findViewById(R.id.circle_new_message);
        mCircleNewImage = (ImageView) view.findViewById(R.id.circle_new_image);
        mTvScheduleNew = (TextView) view.findViewById(R.id.tv_schedule_red_dot);
        mTvApprovalNew = (TextView) view.findViewById(R.id.tv_approval_red_dot);
        mTvWeeklyNew = (TextView) view.findViewById(R.id.tv_weekly_red_dot);
        mTvScanf = (TextView) view.findViewById(R.id.tv_scanf);
        mTvScanf.setOnClickListener(this);
        mMore = (LinearLayout) view.findViewById(R.id.function_ll_more);
        mMore.setOnClickListener(this);

        // 名片部分
        mCardName = (TextView) view.findViewById(R.id.cloud_contact_detail_name);
        mCardPhone = (TextView) view.findViewById(R.id.cloud_contact_detail_phone);
        mCardEmail = (TextView) view.findViewById(R.id.cloud_contact_detail_email);
        mCardCompany = (TextView) view.findViewById(R.id.cloud_contact_detail_company);
        mCardPosition = (TextView) view.findViewById(R.id.cloud_contact_detail_position);
        mCardAddress = (TextView) view.findViewById(R.id.cloud_contact_detail_address);
        mCardPhoto = (ImageView) view.findViewById(R.id.cloud_contact_photo);
        mCardSex = (ImageView) view.findViewById(R.id.cloud_contact_detail_sex);
        mCardPhoto.setOnClickListener(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        newTalkId = mPreferenceUtil.getString("newTalkId", "");
        // TODO 如果此时名片信息还没加载过来，再去请求一次
        nameCard = (ArrayList<String>) mPreferenceUtil.getCloudContact(Constants.CONTACT_NAME_CARD);
        if (nameCard != null && nameCard.size() > 0) {
            setNameCard(nameCard);
        }
        mNewDrafts = mPreferenceUtil.getInt(DraftsActivity.DRAFT_EVENT_UNREAD, 0);
        setNewMsg(mTvMessage, mNewDrafts);
        GetNewMessage();
        GetCalendarStatus();
        getApprovalMsg();
        getWeeklyMsg();
        Log.d("function", "onResume");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("function", "onStart");
    }

    public void setNameCard(ArrayList<String> nameCard) {
        mMyName = nameCard.get(0);
        mName.setText(mMyName);
        mCardName.setText(nameCard.get(0));
        mCardPhone.setText(nameCard.get(1));
        mCardPhone.setTextColor(getResources().getColor(R.color.cloud_contact_detail_blue));
        if (!StringUtil.isEmpty(nameCard.get(3))) {
            mCardCompany.setText(nameCard.get(3));
        }
        if (!StringUtil.isEmpty(nameCard.get(4))) {
            mCardPosition.setVisibility(View.VISIBLE);
            mCardPosition.setText(nameCard.get(4));
        } else {
            mCardPosition.setVisibility(View.GONE);
        }
        if (!StringUtil.isEmpty(nameCard.get(5))) {
            mCardEmail.setText(nameCard.get(5));
        } else {
            mCardEmail.setText("资料未完善");
        }
        if (!StringUtil.isEmpty(nameCard.get(6))) {
            mCardAddress.setText(nameCard.get(6));
        } else {
            mCardAddress.setText("资料未完善");
        }
        mMyAvatar = nameCard.get(7);
        mPreferenceUtil.save(PreferenceUtil.NAMECARD_AVATAR, mMyAvatar);
        mMySex = nameCard.get(8);
        if (!StringUtil.isEmpty(mMyAvatar) && !"None".endsWith(mMyAvatar)) {
            ImageLoaderInterface.imageLoader.displayImage(mMyAvatar, mCardPhoto, ImageLoaderInterface.optionHeadImage);
        }
        if (StringUtil.isEmpty(mMySex) || "0".equals(mMySex)) {
            mCardSex.setVisibility(View.GONE);
        } else if ("1".equals(mMySex)) {
            mCardSex.setVisibility(View.VISIBLE);
            mCardSex.setImageResource(R.drawable.sex_man);
        } else if ("2".equals(mMySex)) {
            mCardSex.setVisibility(View.VISIBLE);
            mCardSex.setImageResource(R.drawable.sex_lady);
        }

    }

    @Override
    public void onClick(View view) {
        if (Constants.isFastClick()) {
            return;
        }
        switch (view.getId()) {
//                case R.id.tm_keypad:  // 设置键盘
//                    showChangeLayoutDialog();
//                    break;
//                case R.id.tm_skin:    // 设置皮肤
//                    mActivity.showSkinListDialog();
//                    break;
            case R.id.tm_circle://人脉圈
                mPreferenceUtil.save("newTalkId", newTalkId);
                mCircleNewMessage.setVisibility(View.GONE);
                Intent intent = new Intent(mActivity, TalkListActivity.class);
                intent.setAction(TalkListActivity.CONTACTS_CIRCLE);
                startActivity(intent);

                break;
            case R.id.tm_my_space://个人空间
                intent = new Intent(mActivity, TalkListActivity.class);
                intent.setAction(TalkListActivity.MY_PERSONAL_SPACE);
                startActivity(intent);
                break;
            case R.id.tm_schedule://共享日程表
                intent = new Intent(mActivity, ScheduleCalendarActivity.class);
                intent.putExtra("userName", mMyName);
                startActivity(intent);
                break;
            case R.id.tm_weekly://共享日程表
                intent = new Intent(mActivity, WeeklyMainActivity.class);
                startActivity(intent);
                break;
            case R.id.tm_attendance: //考勤页面
                intent = new Intent(mActivity, AttendanceMainActivity.class);
                startActivity(intent);
                break;
            case R.id.tm_approval: //审批页面
                intent = new Intent(mActivity, ApprovalActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_scanf://二维码扫描
                intent = new Intent(mActivity,
                      CaptureActivity.class);
                startActivity(intent);
                break;
            case R.id.function_ll_more:
                intent = new Intent(mActivity, EditMyCardActivity.class);
                intent.putStringArrayListExtra("contact_data", nameCard);
                startActivity(intent);
                //showSetPop();
                break;
            case R.id.tm_recommend: // 推荐退队
                showSharePopAlert("推荐\"" + getString(R.string.app_name) + "\"给小伙伴，赶紧加入我们", null);
                break;
            case R.id.tm_setting: //设置页面
                intent = new Intent(mActivity, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.tm_draft: // 草稿箱
                intent = new Intent(mActivity, DraftsActivity.class);
                startActivity(intent);
                break;
            case R.id.tm_pay_tools://付费工具
                Toast.makeText(mActivity, "暂未开通,请耐心等待", Toast.LENGTH_LONG).show();
                /*intent = new Intent(mActivity, PayToolsActivity.class);
                startActivity(intent);*/
                break;
            case R.id.cloud_contact_photo:
                ArrayList<String> image = new ArrayList<String>();
                image.add(mMyAvatar.split("\\?")[0]);
                Intent i = new Intent(mActivity, ImagePagerActivity.class);
                i.putExtra(Constants.IMAGE_ORIGIN, Constants.IMAGE_ORIGIN_URL_ARRAY);
                i.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, image);
                startActivity(i);
                break;
            default:
                break;
        }
    }

    /**
     * 弹出分享面板
     */
    public void showSharePopAlert(String shareText, Bitmap shareImage) {
        View mainView = LayoutInflater.from(mActivity).inflate(R.layout.share_pop, null);
        GridView gr_share = (GridView) mainView.findViewById(R.id.gr_share);
        LinearLayout shareLayout = (LinearLayout) mainView.findViewById(R.id.qrcode_ll_layout);
        TextView shareTvCancel = (TextView) mainView.findViewById(R.id.share_tv_cancel);

        mShareWindow = new PopupWindow(mActivity);
        mShareWindow.setBackgroundDrawable(new BitmapDrawable());
        mShareWindow.setFocusable(true);
        mShareWindow.setTouchable(true);
        mShareWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        mShareWindow.setContentView(mainView);
        mShareWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mShareWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        //mShareWindow.setAnimationStyle(R.style.bottomStyle);
        //shareLayout.startAnimation(AnimationUtils.loadAnimation(mActivity,R.anim.bottom_pup_anim_in));

        ArrayList<HashMap<String, Object>> gridItem = new ArrayList<HashMap<String, Object>>();
        int[] drawableId = {R.drawable.sms_iv_selector, R.drawable.qrcode_iv_selector, R.drawable.wx_chat_iv_selector, R.drawable.wx_circle_iv_selector};
        String[] drawableText = {"短信邀请", "二维码邀请", "微信好友", "朋友圈"};
        for (int i = 0; i < 4; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", drawableId[i]);
            map.put("ItemText", drawableText[i]);
            gridItem.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(mActivity,
              gridItem,
              R.layout.share_grid_item,
              new String[]{"ItemImage", "ItemText"},
              new int[]{R.id.item_image, R.id.item_text});
        gr_share.setAdapter(simpleAdapter);
        gr_share.setOnItemClickListener(new ItemClickListener(shareText, shareImage));

        shareLayout.startAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.pop_translate_in));
        mShareWindow.showAtLocation(mainView, Gravity.BOTTOM, 0, 0);
        //mShareWindow.update();

        shareTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mShareWindow.dismiss();
            }
        });

        mainView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mShareWindow.dismiss();
                return false;
            }
        });

    }

    class ItemClickListener implements AdapterView.OnItemClickListener {

        String textContent;//要分享的文字内容
        Bitmap imageContent;//要分享的图片内容

        public ItemClickListener(String text, Bitmap bitmap) {
            this.textContent = text;
            this.imageContent = bitmap;
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            switch (i) {
                case 0:
                    Intent sms = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + ""));
                    sms.putExtra("sms_body", mMyName + "邀请你使用" + getString(R.string.app_name) + ",点击" + AsynHttpClient.HTTP_TEAM_DOWNLOAD + " 接受邀请");
                    startActivity(sms);
                    mShareWindow.dismiss();
                    break;
                case 1:
                    Intent intent = new Intent(mActivity, MyShareQrCodeActivity.class);
                    startActivity(intent);
                    mShareWindow.dismiss();
                    break;
                case 2:
                    mShareUtil.shareToWx(getString(R.string.app_name) + " - 让企业沟通更高效", textContent, null, 2, imageContent);
                    mShareWindow.dismiss();
                    break;
                case 3:
                    mShareUtil.shareToWxCircle(textContent, null, imageContent);
                    mShareWindow.dismiss();
                    break;


            }

        }
    }

    /**
     * 设置键盘
     */
    private void showChangeLayoutDialog() {
//        mActivity.mSendCardShaker.pause();
        final Dialog lDialog = new Dialog(mActivity, android.R.style.Theme_Translucent_NoTitleBar);
        WindowManager.LayoutParams lp = lDialog.getWindow().getAttributes();
        lp.dimAmount = 0.f;
        lDialog.getWindow().setAttributes(lp);
        lDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(R.layout.dialog_change_layout);
        lDialog.setCanceledOnTouchOutside(true);
        final RadioButton radioBtn_v = (RadioButton) lDialog.findViewById(R.id.isSelRow6);
        final RadioButton radioBtn_h = (RadioButton) lDialog.findViewById(R.id.isSelRow7);
        int keyboard_layout_orientation = mPreferenceUtil.get(
              PreferenceUtil.CONTACT_KEYBOARD_LAYOUTORIENTATION, Integer.class);
        switch (keyboard_layout_orientation) {
            case Constants.KEYBOARD_LAYOUT_VERTICAL:
                radioBtn_v.setChecked(true);
                break;
            case Constants.KEYBOARD_LAYOUT_HORIZONTAL:
                radioBtn_h.setChecked(true);
                break;
            default:
                radioBtn_v.setChecked(true);
                break;
        }
        View view_vertical = (View) lDialog.findViewById(R.id.group_vertical);
        view_vertical.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                lDialog.dismiss();
                radioBtn_v.setChecked(true);
                radioBtn_h.setChecked(false);
                mPreferenceUtil.save(PreferenceUtil.CONTACT_KEYBOARD_LAYOUTORIENTATION, Constants.KEYBOARD_LAYOUT_VERTICAL);
//                mActivity.setKeyBoardOrientation(Constants.KEYBOARD_LAYOUT_VERTICAL);
//                mActivity.mSendCardShaker.resume();
            }
        });
        View view_horizontal = (View) lDialog.findViewById(R.id.group_horizontal);
        view_horizontal.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                lDialog.dismiss();
                radioBtn_h.setChecked(true);
                radioBtn_v.setChecked(false);
                mPreferenceUtil.save(PreferenceUtil.CONTACT_KEYBOARD_LAYOUTORIENTATION, Constants.KEYBOARD_LAYOUT_HORIZONTAL);
//                mActivity.setKeyBoardOrientation(Constants.KEYBOARD_LAYOUT_HORIZONTAL);
//                mActivity.mSendCardShaker.resume();
            }
        });
        Button btnExit = (Button) lDialog.findViewById(R.id.btn_exit);
        btnExit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                lDialog.dismiss();
//                mActivity.mSendCardShaker.resume();
            }
        });
        lDialog.show();
    }


    /**
     * 弹出编辑和分享 暂时去掉 呵呵哒
     */
    private void showSetPop() {
        View mainView = LayoutInflater.from(mActivity).inflate(R.layout.function_more_window, null);
        Button btnShare = (Button) mainView.findViewById(R.id.function_more_share);
        // 分享名片
        btnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                mActivity.mShareContactId = mContactId;
                Intent intent = new Intent(mActivity, CloudDialerActivity.class);
                intent.setAction("share_contact");
                intent.putExtra(Constants.CONTACT_ID_KEY, 222L);
                intent.putExtra(Constants.CONTACT_NAME_KEY, nameCard.get(0));
                if (StringUtil.isEmpty(nameCard.get(2))) {
                    intent.putExtra(Constants.CONTACT_PHONE_KEY, "电话：" + nameCard.get(1));
                } else {
                    intent.putExtra(Constants.CONTACT_PHONE_KEY, "电话：" + nameCard.get(1) + "\n" + "电话：" + nameCard.get(2));
                }
                mActivity.startActivity(intent);
                mMoreWindow.dismiss();
            }
        });
        Button btnEdit = (Button) mainView.findViewById(R.id.function_more_edit);
        // 编辑名片
        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, EditMyCardActivity.class);
                intent.putStringArrayListExtra("contact_data", nameCard);
                startActivity(intent);
                mMoreWindow.dismiss();
            }
        });
        Button btnCancel = (Button) mainView.findViewById(R.id.function_more_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMoreWindow.dismiss();
            }
        });

        mMoreWindow = new PopupWindow(mActivity);
        mMoreWindow.setBackgroundDrawable(new BitmapDrawable());
        mMoreWindow.setFocusable(true);
        mMoreWindow.setTouchable(true);
        mMoreWindow.setOutsideTouchable(true);
        mMoreWindow.setContentView(mainView);
        mMoreWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mMoreWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mMoreWindow.setAnimationStyle(R.style.bottomStyle);
        mMoreWindow.showAtLocation(mainView, Gravity.BOTTOM, 0, 0);
        mMoreWindow.update();

        mainView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mMoreWindow.dismiss();
                return false;
            }
        });
    }

    //回调机制更新此Fragment的方法
    public void update() {
        mNewDrafts = mPreferenceUtil.getInt(DraftsActivity.DRAFT_EVENT_UNREAD, 0);
        setNewMsg(mTvMessage, mNewDrafts);
    }

    /**
     * 获取人脉圈消息
     */
    private void GetNewMessage() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        if (!StringUtil.isEmpty(newTalkId)) {
            nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_TALK_ID, newTalkId));
        }
        GetTalkNewMsg message = new GetTalkNewMsg(mHandler.obtainMessage(GET_TALK_NEW), mActivity);
        message.mApi = AsynHttpClient.API_TALK_NEW;
        message.mParams = nvps;
        AsynHttpClient.getInstance(mActivity).execTalkHttp(message);
    }

    /**
     * 获取日程消息
     */
    private void GetCalendarStatus() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        GetCalendarStatusMsg message = new GetCalendarStatusMsg(mHandler.obtainMessage(GET_CALENDAR_STATUS), mActivity);
        message.mApi = AsynHttpClient.API_EVENT_CALENDAR_STATUS;
        message.mParams = nvps;
        AsynHttpClient.getInstance(mActivity).execEventHttp(message);
    }

    /**
     * 获取审批消息
     */
    private void getApprovalMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        GetCalendarStatusMsg message = new GetCalendarStatusMsg(mHandler.obtainMessage(GET_CALENDAR_STATUS), mActivity);
        message.mApi = AsynHttpClient.API_EVENT_APPROVAL_TODO;
        message.mParams = nvps;
        AsynHttpClient.getInstance(mActivity).execEventHttp(message);
    }

    /**
     * 获取周报消息
     */
    private void getWeeklyMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        GetCalendarStatusMsg message = new GetCalendarStatusMsg(mHandler.obtainMessage(GET_CALENDAR_STATUS), mActivity);
        message.mApi = AsynHttpClient.API_EVENT_WEEKLY_ALERT;
        message.mParams = nvps;
        AsynHttpClient.getInstance(mActivity).execEventHttp(message);
    }

    /**
     * 获取名片信息
     */
    private void uploadNameCard() {

        List<NameValuePair> mList = new ArrayList<NameValuePair>();
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        NameCardMessage message = new NameCardMessage(mHandler.obtainMessage(MSG_GET_NAMECARD), mActivity);
        message.mApi = AsynHttpClient.API_USER_MYNAMECARD;
        message.mParams = mList;
        AsynHttpClient.getInstance(mActivity).execUserHttp(message);

    }


}